{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "992f84ef-61c9-4512-8e6e-5d510d5f07f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import time\n",
    "import base64\n",
    "\n",
    "from multi_agent import agent\n",
    "from xunfei_ds import DeepSeekMain\n",
    "from zhipuai_vl import GLM4V"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9b899567-8292-4fb4-af85-8ba7deb7f926",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MsgProcessor:\n",
    "    msg_len = 10 # 控制超过10个字开始转语音\n",
    "    message = \"\"\n",
    "    status = 0 # 0: 处理完成；1：处理中\n",
    "    audio_id = 0 # 生成语音的编号 \n",
    "    \n",
    "    @staticmethod\n",
    "    def processing(msg):\n",
    "        # 将msg中的逗号统一替换成句号, 方便控制句子长度.\n",
    "        msg = msg.replace(\"，\", \"。\")\n",
    "        MsgProcessor.message += msg\n",
    "\n",
    "        # 这里需要判断积累到多少字或者遇到逗号/句号时开始转语音。TODO: 判断逻辑需要不断优化。\n",
    "        msg_split = MsgProcessor.message.split(\"。\")\n",
    "        audio_msg = \",\".join(msg_split[:-1])\n",
    "        if len(audio_msg) >= MsgProcessor.msg_len:\n",
    "            MsgProcessor.message = msg_split[-1]\n",
    "            print(f\"audio_msg={audio_msg}, MsgProcessor.message={MsgProcessor.message}\")\n",
    "            MsgProcessor.audio_id += 1\n",
    "            agent.text2audio(audio_msg, f\"audio_out_{MsgProcessor.audio_id}.wav\")\n",
    "\n",
    "    @staticmethod\n",
    "    def processed():\n",
    "        MsgProcessor.message = MsgProcessor.message.strip()\n",
    "        if len(MsgProcessor.message) != 0:\n",
    "            print(f\"MsgProcessor.message={MsgProcessor.message}=={len(MsgProcessor.message)}\")\n",
    "            MsgProcessor.audio_id += 1\n",
    "            agent.text2audio(MsgProcessor.message, f\"audio_out_{MsgProcessor.audio_id}.wav\")\n",
    "        MsgProcessor.message = \"\"\n",
    "        MsgProcessor.status = 0\n",
    "        MsgProcessor.audio_id = 0\n",
    "\n",
    "tmp = []\n",
    "def on_message(ws, message):\n",
    "    data = json.loads(message)\n",
    "    code = data[\"header\"][\"code\"]\n",
    "    if code != 0:\n",
    "        print(f\"请求错误: {code}, {data}\")\n",
    "        ws.close()\n",
    "    else:\n",
    "        choices = data[\"payload\"][\"choices\"]\n",
    "        status = choices[\"status\"]\n",
    "        content = choices[\"text\"][0][\"content\"]\n",
    "        #print(f\"content={content}\")\n",
    "        #tmp.append(content)\n",
    "        #print(tmp)\n",
    "        MsgProcessor.processing(content)\n",
    "        #print(content, end=\"~\")\n",
    "        if status == 2:\n",
    "            MsgProcessor.processed()\n",
    "            ws.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fa98e607-2623-4835-9719-0cbb25f9541d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "ws open \n",
      "audio_msg=成都位于四川盆地,是四川省省会, MsgProcessor.message=\n",
      "text2audio: saved=audio_out_1.wav\n",
      "audio_msg=这里气候温和,美食丰富,有都江堰、青城山等著名景点, MsgProcessor.message= \n",
      "text2audio: saved=audio_out_2.wav\n",
      "\n",
      "ws close \n",
      "==================================================\n",
      "MsgProcessor.message:  \n"
     ]
    }
   ],
   "source": [
    "DeepSeekMain.chat(\"给我介绍一下成都。\", on_message)\n",
    "print(\"=\"*50)\n",
    "print(\"MsgProcessor.message: \", MsgProcessor.message)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ffa21655-cfac-4a71-ba4b-8deddbd864f8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "ws open \n",
      "请求错误: 11203, {'header': {'code': 11203, 'message': 'AppIdConcurrencyOverFlowError:(18:49:37.267) `275f9e51`SUBID_275f9e51_1234`xdeepseekv3`tokens.total`125`&`|`tokens.total`_D`#`-1`P`_C`L`-1`#`-1`P`_Q`L`2`q`1`P`_CO`b`5`d`5`S`5`AQC`N', 'sid': 'cht000b801b@dx1956b133af2b8f2700', 'status': 2}}\n",
      "\n",
      "ws close \n",
      "[INFO]站起来==\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]起来工作了==up\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]休息一下吧==down\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]向左走==left\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]向右转==right\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]前进==forward\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]往前走==forward\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]后退==back\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]带我们去指挥中心==navigation-指挥中心\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]导航到出发点==navigation-出发点\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]你看到了什么？==vision\n",
      "============================\n",
      "audio_msg=我看到了一群人在户外活动, MsgProcessor.message=\n",
      "text2audio: saved=audio_out_1.wav\n",
      "audio_msg=其中一些人正在骑摩托车, MsgProcessor.message=\n",
      "text2audio: saved=audio_out_2.wav\n",
      "audio_msg=另一些人则在旁边观看或交谈, MsgProcessor.message=\n",
      "text2audio: saved=audio_out_3.wav\n",
      "audio_msg=背景中有绿色的树木和一些告示牌, MsgProcessor.message=\n",
      "text2audio: saved=audio_out_4.wav\n",
      "\n",
      "ws open \n",
      "\n",
      "ws close \n",
      "[INFO]你面前有多少人？==vision\n",
      "============================\n",
      "MsgProcessor.message=我看到了十个人。==8\n",
      "text2audio: saved=audio_out_1.wav\n"
     ]
    }
   ],
   "source": [
    "with open(\"test.jpg\", 'rb') as img_file:\n",
    "    img_base = base64.b64encode(img_file.read()).decode('utf-8')\n",
    "    \n",
    "test_list = [\"站起来\", \"起来工作了\", \"休息一下吧\", \"向左走\", \"向右转\", \"前进\", \"往前走\", \"后退\", \"带我们去指挥中心\", \"导航到出发点\", \"你看到了什么？\", \"你面前有多少人？\"]\n",
    "for question in test_list:\n",
    "    cmd = DeepSeekMain.cmd(question)\n",
    "    print(f\"[INFO]{question}=={cmd}\")\n",
    "    if cmd == \"vision\":\n",
    "        print(\"============================\")\n",
    "        contents = GLM4V.chat(question, img_base)\n",
    "        for content in contents:\n",
    "            # print(content)\n",
    "            MsgProcessor.processing(content)\n",
    "        MsgProcessor.processed()\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "98e57331-6267-4a59-9253-0054c04ff8bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我看到了一群人在户外广场上。其中一些人正在互动。还有几辆摩托车停在旁边'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"我看到了一群人在户外广场上，其中一些人正在互动，还有几辆摩托车停在旁边\".replace(\"，\", \"。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "05a5f7f5-e5e3-47a3-81aa-7d2174f6e12d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'12'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\".join(['1','2'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5cbcbed7-9f42-4fd0-848a-94276ca488eb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
